package XML_;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * @Author 汤舒青
 * @Version 1.0
 */


public class BookHsndler {
    public static void main(String[] args) {
        SAXReader reader = new SAXReader();
        String filePath="D:\\IdeaProjects\\javau1\\java网络编程\\src\\XML_\\Book.xml";
        try {
            Document doc = reader.read(new FileInputStream(filePath));
            Element root = doc.getRootElement();
            List<Element> bookList = root.elements("book");
            Book book=null;
            for (Element element : bookList) {
                String type = element.attributeValue("type");
                String author = element.elementTextTrim("author");
                String year = element.elementTextTrim("year");
                String price = element.elementTextTrim("price");
            book=new Book();
            book.setType(type);
            book.setAuthor(author);
            book.setYear(Integer.parseInt(year));
            book.setPrice(Double.parseDouble(price));
                System.out.println(book.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
